package com.dgut.lotterySystem.controller.handle;

import com.dgut.lotterySystem.common.errorcode.GlobalErrorCodeConstants;
import com.dgut.lotterySystem.common.pojo.exception.ControllerException;
import com.dgut.lotterySystem.common.pojo.exception.ServiceException;
import com.dgut.lotterySystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandle {

    private final static Logger logger=LoggerFactory.getLogger(GlobalExceptionHandle.class);

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceExceptionHandle(ServiceException e){
        logger.error("ServiceException"+e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerExceptionHandle(ControllerException e){
        logger.error("ControllerException"+e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exceptionHandle(Exception e){
        logger.error("Exception"+e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }
}
